package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.Host;
import com.rupertjones.globalcron.domain.Setting;
import com.rupertjones.globalcron.web.service.JobService;
import com.rupertjones.globalcron.web.service.SettingsService;
import com.rupertjones.globalcron.web.service.TimezoneService;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Collection;
import java.util.List;

import static com.rupertjones.globalcron.domain.SettingType.ALLOW_AD_HOC_EXECUTION;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
public abstract class AbstractAction implements ActionBean {

    @SpringBean
    private JobService jobService;

    @SpringBean
    private TimezoneService timezoneService;

    private ActionBeanContext context;

    @SpringBean
    private SettingsService settingsService;

    @Override
    public void setContext(ActionBeanContext actionBeanContext) {
        this.context = actionBeanContext;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    protected JobService getJobService() {
        return jobService;
    }

    protected SettingsService getSettingsService() {
        return settingsService;
    }

    public Collection<Host> getHosts() {
        return getJobService().getHosts();
    }

    public Collection<Setting> getSettings() {
        return getSettingsService().findAll();
    }

    public boolean isEmpty(String value) {
        return !(value != null && value.length() > 0);
    }

    public String getParameterValue(String key) {
        return getContext().getRequest().getParameter(key);
    }

    public Host getCurrentHost() {
        return jobService.getCurrentHost();
    }

    public List<String> getWorldClock() {
        return timezoneService.currentTimeInCurrentlyUsedZones();
    }

    public boolean isAllowAdHocExecution() {
        return (Boolean) settingsService.getSettingValue(ALLOW_AD_HOC_EXECUTION);
    }
}
